'use strict';

const bcrypt = require('bcryptjs');
const moment = require('moment/moment');
/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up(queryInterface, Sequelize) {
    const generateDetailedUserData = (count = 100) => {
      const users = [];
      const domains = ['clwy.cn', 'example.com', 'test.org', 'demo.net'];
      const nicknames = [
        '小明',
        '小红',
        '小刚',
        '小李',
        '小张',
        '小赵',
        '小钱',
        '小孙',
        '小周',
        '小吴',
      ];
      const roles = [0, 1, 100];
      const sexOptions = [0, 1, 2];

      for (let i = 1; i <= count; i++) {
        const sex = sexOptions[Math.floor(Math.random() * sexOptions.length)];
        const role = roles[Math.floor(Math.random() * roles.length)];
        const domain = domains[Math.floor(Math.random() * domains.length)];
        const nickname = nicknames[Math.floor(Math.random() * nicknames.length)] + i;

        // 生成2024-2025年间的随机日期，确保覆盖每个月
        const year = Math.random() > 0.5 ? 2024 : 2025;
        const month = Math.floor(Math.random() * 12);
        const day = Math.floor(Math.random() * 28) + 1; // 1-28日，避免日期越界
        const hour = Math.floor(Math.random() * 24);
        const minute = Math.floor(Math.random() * 60);
        const second = Math.floor(Math.random() * 60);

        const createdAt = new Date(year, month, day, hour, minute, second);
        const updatedAt = new Date(createdAt.getTime() + Math.random() * 15 * 24 * 60 * 60 * 1000); // 15天内更新

        const user = {
          email: `user${i}@${domain}`,
          username: `user${i}`,
          password: bcrypt.hashSync('123456', 10),
          nickname: nickname,
          sex: sex,
          role: role,
          membershipExpiredAt: role == 1 ? moment().add(1, 'year').toDate() : null,
          createdAt: createdAt,
          updatedAt: updatedAt,
        };
        users.push(user);
      }
      return users;
    };
    await queryInterface.bulkInsert('Users', generateDetailedUserData(), {});
  },

  async down(queryInterface, Sequelize) {
    await queryInterface.bulkDelete('Users', null, {});
  },
};
